package com.rc.evcharger.service.elec;

import com.alibaba.fastjson.JSON;
import com.rc.evcharger.common.type.ElecStationPriceData;
import com.rc.evcharger.common.utils.DateHelper;
import com.rc.evcharger.common.utils.ToFrameUtil;
import com.rc.evcharger.mapper.elec.ElecStationPriceMapper;
import com.rc.evcharger.model.elec.ElecStationPrice;
import com.rc.evcharger.model.elec.ElecStationPriceExample;
import com.rc.evcharger.model.order.OrderInfo;
import com.rc.evcharger.redis.RedChargeDataService;
import com.rc.evcharger.vo.price.ElecStationPriceAssember;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by sven on 2019/09/23.
 */
@Service
public class ElecStationPriceServiceImpl implements ElecStationPriceService {

    @Autowired
    private ElecStationPriceMapper elecStationPriceMapper;

    @Autowired
    private RedChargeDataService redChargeDataService;

    @Override
    public ElecStationPriceMapper getMapper() {
        return elecStationPriceMapper;
    }

    /**
     * 得到当前时间的有效时段电价
     *
     * @param elecStationId
     * @return
     */
    @Override
    public ElecStationPrice selectValidElecStationPrice(Long elecStationId) {
        return elecStationPriceMapper.queryValidElecStationPrice(elecStationId);
    }

    /**
     * 获取指定时段的电价
     *
     * @param ruta
     * @param gunNum
     * @param orderNum
     * @return
     */
    @Override
    public ElecStationPrice selectRedValidElecStationPriceByDate(String ruta, String gunNum, String orderNum, Date date) {
        String orderInfoPrices = redChargeDataService.getOrderInfoPrices(ruta, gunNum, orderNum);
        if (orderInfoPrices == null) {
            return null;
        }

        ElecStationPriceAssember elecStationPriceAssember = JSON.parseObject(orderInfoPrices, ElecStationPriceAssember.class);
        List<ElecStationPrice> elecStationPrices = elecStationPriceAssember.getElecStationPrices();


        ElecStationPrice elecStationPrice = null;
        SimpleDateFormat sdf = new SimpleDateFormat(DateHelper.DATE_FORMAT_HHMMSS);
        try {
            for (ElecStationPrice price : elecStationPrices) {
                Date dateParam = sdf.parse(sdf.format(date));
                Date start = price.getBeginTime();
                Date end = price.getEndTime();
                if (dateParam.getTime() >= start.getTime() && dateParam.getTime() <= end.getTime()) {
                    elecStationPrice = price;
                    break;
                }
                //如果结束时间为00.00;
                if (end.getTime() == -28800000) {
                    elecStationPrice = price;
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return elecStationPrice;
    }

    @Override
    public ElecStationPrice selectHlhtValidElecStationPrice(Long elecStationId, OrderInfo orderInfo) {
        ElecStationPrice elecStationPrice = elecStationPriceMapper.queryValidElecStationPrice(elecStationId);
        return elecStationPrice;
    }

    @Override
    public List<ElecStationPrice> listByStationId(Long elecStationId) {

        ElecStationPriceExample elecStationPriceExample = new ElecStationPriceExample();
        elecStationPriceExample.createCriteria().andElecStationIdEqualTo(elecStationId);

//        elecStationPriceExample.setOrderByClause("begin_time price_id asc");
        List<ElecStationPrice> elecStationPrices = elecStationPriceMapper.selectByExample(elecStationPriceExample);

        return elecStationPrices;
    }

    @Override
    public List<ElecStationPrice> listByStationIdAndCurrent(Long elecStationId) {
        return elecStationPriceMapper.listByStationIdAndCurrent(elecStationId);

    }

    @Override
    public List<ElecStationPriceData> listElecStationPriceDataByStationId(Long elecStationId) {
        List<ElecStationPrice> elecStationPrices = this.listByStationId(elecStationId);
        if (CollectionUtils.isEmpty(elecStationPrices)) {
            return Collections.emptyList();
        }

        Collections.sort(elecStationPrices, new Comparator<ElecStationPrice>() {

            @Override
            public int compare(ElecStationPrice o1, ElecStationPrice o2) {
                int i = o1.getBeginTime().compareTo(o2.getEndTime());
                if (i > 0) {
                    return 1;
                }
                return -1;
            }
        });

        List<ElecStationPriceData> elecStationPriceDataArrayList = new ArrayList<>();
        for (ElecStationPrice elecStationPrice : elecStationPrices) {
            ElecStationPriceData elecStationPriceData = new ElecStationPriceData();

            String beginHour = DateHelper.dataToString(elecStationPrice.getBeginTime(), DateHelper.DATE_FORMAT_HH);
            String beginMin = DateHelper.dataToString(elecStationPrice.getBeginTime(), DateHelper.DATE_FORMAT_MM);
            if (beginMin.equals("59")) {
                beginMin = "00";
                beginHour = String.valueOf(Integer.valueOf(beginHour) + 1);
                if (beginHour.equals("24")) {
                    beginHour = "00";
                }
                if (beginHour.length() == 1) {
                    beginHour = "0" + beginHour;
                }
                if (beginMin.length() == 1) {
                    beginMin = "0" + beginMin;
                }
            }
            //解析时段
            elecStationPriceData.setBeginHour(beginHour);
            elecStationPriceData.setBeginMinute(beginMin);

            String endHour = DateHelper.dataToString(elecStationPrice.getEndTime(), DateHelper.DATE_FORMAT_HH);
            String endMin = DateHelper.dataToString(elecStationPrice.getEndTime(), DateHelper.DATE_FORMAT_MM);
            if (endMin.equals("59")) {
                endMin = "00";
                endHour = String.valueOf(Integer.valueOf(endHour) + 1);
                if (endHour.equals("24")) {
                    endHour = "00";
                }
                if (endHour.length() == 1) {
                    endHour = "0" + endHour;
                }
                if (endMin.length() == 1) {
                    endMin = "0" + endMin;
                }
            }
            elecStationPriceData.setEndHour(endHour);
            elecStationPriceData.setEndMinute(endMin);

            //解析电价
            elecStationPriceData.setElecPrice(ToFrameUtil.accountToFrame(elecStationPrice.getElecPrice()));
            elecStationPriceData.setServicePrice(ToFrameUtil.accountToFrame(elecStationPrice.getServicePrice()));
            elecStationPriceDataArrayList.add(elecStationPriceData);
        }

        return elecStationPriceDataArrayList;
    }

    @Override
    public ElecStationPrice selectValidElecStationPriceByDate(Long elecStationId, Date date) {
        List<ElecStationPrice> elecStationPrices = this.listByStationId(elecStationId);

        ElecStationPrice elecStationPrice = null;
        SimpleDateFormat sdf = new SimpleDateFormat(DateHelper.DATE_FORMAT_HHMMSS);
        try {
            for (ElecStationPrice price : elecStationPrices) {
                Date dateParam = sdf.parse(sdf.format(date));
                Date start = price.getBeginTime();
                Date end = price.getEndTime();
                if (dateParam.getTime() >= start.getTime() && dateParam.getTime() <= end.getTime()) {
                    elecStationPrice = price;
                    break;
                }
                //如果结束时间为00.00;
                if (end.getTime() == -28800000) {
                    elecStationPrice = price;
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return elecStationPrice;
    }


    public static void main(String[] args) throws Exception {
        List<ElecStationPrice> elecStationPrices = new ArrayList<>();
        ElecStationPrice elecStationPrice1 = new ElecStationPrice();
        elecStationPrice1.setBeginTime(new Date());
        Thread.sleep(1000l);
        ElecStationPrice elecStationPrice2 = new ElecStationPrice();
        elecStationPrice2.setBeginTime(new Date());

        elecStationPrices.add(elecStationPrice1);
        elecStationPrices.add(elecStationPrice2);

        Collections.sort(elecStationPrices, new Comparator<ElecStationPrice>() {

            @Override
            public int compare(ElecStationPrice o1, ElecStationPrice o2) {
                int i = o1.getBeginTime().compareTo(o2.getBeginTime());
                if (i > 0) {
                    return 1;
                }
                return -1;
            }
        });

        for (ElecStationPrice elecStationPrice : elecStationPrices) {
            System.out.println(DateHelper.dataToString(elecStationPrice.getBeginTime(), DateHelper.DATE_COMMON_FORMAT));
        }
    }
}
